/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dk.fe;

import java.awt.Point;
import javax.swing.JComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.Widget;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//dk.fe//TH//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "THTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "dk.fe.THTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_THAction",
        preferredID = "THTopComponent")
@Messages({
    "CTL_THAction=TH",
    "CTL_THTopComponent=TH Window",
    "HINT_THTopComponent=This is a TH window"
})
public final class THTopComponent extends TopComponent {

    private TargetScene scene;

    public THTopComponent() {
        initComponents();
        setName(Bundle.CTL_THTopComponent());
        setToolTipText(Bundle.HINT_THTopComponent());
        
        scene = new TargetScene();
        JComponent view = scene.createView();
        scenePane.setViewportView(view);
        
        testAdd();
    }

    private void testAdd()
    {
        Target t=new Target("asdf");
        Widget w=scene.addNode(t);
        w.setPreferredLocation(new Point(40,40));
        
        Target t2=new Target("target2");
        Widget w2=scene.addNode(t2);
        w2.setPreferredLocation(new Point(280,30));
        
        TargetEdge e=new TargetEdge();
        ConnectionWidget c = (ConnectionWidget) scene.addEdge(e);
        c.setSourceAnchor (AnchorFactory.createRectangularAnchor (w));
        c.setTargetAnchor (AnchorFactory.createRectangularAnchor (w2));
        
        scene.validate();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scenePane = new javax.swing.JScrollPane();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scenePane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scenePane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scenePane;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }
    
    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }
    
    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }
    
    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}
